package controller

import (
	"base/app/utils"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strconv"
	"strings"
	"time"
)

type upCtl struct {
	db *gorm.DB
}

var Up = upCtl{}

// 接受上传的文件，保存到指定目录
func (u *upCtl) Upload(c *gin.Context) {
	file, _ := c.FormFile("file")
	// 上传文件至指定目录
	nowDateStr := utils.GetNowDate()
	//生成时间戳
	timeStamp := time.Now().Unix()
	//生成文件名
	fileName := strings.Split(file.Filename, ".")[0] + "_" + strconv.FormatInt(timeStamp, 10) + "." + strings.Split(file.Filename, ".")[1]
	filePath := "./public/img/" + nowDateStr + "/" + fileName
	err := c.SaveUploadedFile(file, filePath)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "上传失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "上传成功",
		"data": strings.TrimLeft(filePath, "."),
	})
}

//删除指定路径的文件
//func (u *upCtl) DeleteFile(c *gin.Context) {
//	filePath := c.Query("filePath")
////
//}
